﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;
using KorbeRTR.Utility;

namespace KorbeRTR
{
    public partial class FormAddProduct : Form
    {
        public FormProductInfo form;

        private RecipeView CalcRecipe;

        private RecipeView Recipe;

        private bool IsChangeRecipe;

        public FormAddProduct(FormProductInfo frm)
        {
            InitializeComponent();
            form = frm;

            IsChangeRecipe = false;
        }
        public FormAddProduct(FormProductInfo frm, RecipeView recipe, bool isChangeRecipe)
        {
            InitializeComponent();
            form = frm;

            Recipe = recipe;
            IsChangeRecipe = isChangeRecipe;
        }

        private void FormAddProduct_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void FormAddProduct_Load(object sender, EventArgs e)
        {
            CalcRecipe = new RecipeView()
            {
                D电流效率 = 0.95,
                K卡板报警电压 = 5,
            };
            for(int i=0; i<Config.镀铜段数; i++)
            {
                CalcRecipe.CuSlots[i].D电流系数 = 1;
            }

            //修改
            if (IsChangeRecipe)
            {
                var iter = Recipe;

                txt料号.Text = iter.Name;
             

                txt宽度.Text = iter.Width.ToString();

                txt电镀效率.Text = (iter.D电流效率 * 100).ToString();
                txt板厚.Text = iter.Thickness.ToString("f2");

                txt镀铜时间.Text = iter.D镀铜时间.ToString();

                //这个地方没有写入公式
                txt镀铜C面厚度.Text = iter.D镀铜C面厚度.ToString();
                txt镀铜S面厚度.Text = iter.D镀铜S面厚度.ToString();
       
                txt卡板报警电压.Text = iter.K卡板报警电压.ToString("f2");
                txt除油1频率.Text = iter.C除油1频率.ToString();
                txt除油2频率.Text = iter.C除油2频率.ToString();

                rht备注.Text = iter.Comment;
                

                //镀铜
                {
                    var type = this.GetType();
                    var Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                    for (int i = 1; i <= KorbeRTR.Config.镀铜段数; i++)
                    {
                        int slot = i - 1;

                        var tb = type.GetField("tbxCurrentCoefficient" + i.ToString(), Flags).GetValue(this) as TextBox;
                        tb.Text = iter.CuSlots[slot].D电流系数.ToString("f2");

                        tb = type.GetField("tbxFlow" + i.ToString(), Flags).GetValue(this) as TextBox;
                        tb.Text = iter.CuSlots[slot].Flow.ToString();
                    }
                }

                txt建立用户.Text = Global.GetInstance().CurrentUser.UserName;
                txt建立时间.Text = iter.Time.ToString("yyyy-MM-dd HH:mm:ss").ToString();
            }
            else
            {
                txt电镀效率.Text = "95";

                txt建立用户.Text = Global.GetInstance().CurrentUser.UserName;
                txt建立时间.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString();
            }
        }

      

        private void btn保存_Click(object sender, EventArgs e)
        {
            if (!Global.GetInstance().CurrentUser.Check产品基本数据操作权限())
            {
                MessageBox.Show("您没有产品基本数据操作权限, 请联系管理员!", "提示");
                return;
            }

            var ret = MessageBox.Show("确定要保存", "保存", MessageBoxButtons.YesNo);
            if (ret != DialogResult.Yes)
            {
                return;
            }

            txt建立时间.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString();

            //修改
            if (IsChangeRecipe)
            {
                var iter = Recipe;

                iter.Name = txt料号.Text;

                iter.Width = txt宽度.Text.ToDouble();

                iter.D电流效率 = txt电镀效率.Text.ToDouble() / 100;
                iter.Thickness = txt板厚.Text.ToDouble();
                iter.D镀铜C面厚度 = txt镀铜C面厚度.Text.ToDouble();
                iter.D镀铜S面厚度 = txt镀铜S面厚度.Text.ToDouble();

                iter.D镀铜时间 = txt镀铜时间.Text.ToDouble();
                iter.D镀铜速度mpmin = txt镀铜速度米每分.Text.ToDouble();
                iter.C面电流密度ASD = txtC面电流密度.Text.ToDouble();
                iter.S面电流密度ASD = txtS面电流密度.Text.ToDouble();

       //         iter.K卡板报警电压 = txt卡板报警电压.Text.ToDouble();
                iter.C除油1频率 = txt除油1频率.Text.ToDouble();
                iter.C除油2频率 = txt除油2频率.Text.ToDouble();


                iter.Time = DateTime.Now;//时间
                iter.User = Global.GetInstance().CurrentUser.UserName;//用户
                iter.Comment = rht备注.Text;

                //镀铜
                {
                    var type = this.GetType();
                    var Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                    for (int i = 1; i <= KorbeRTR.Config.镀铜段数; i++)
                    {
                        int slot = i - 1;

                        var tb = type.GetField("tbxCurrentCoefficient" + i.ToString(), Flags).GetValue(this) as TextBox;
                        iter.CuSlots[slot].D电流系数 = tb.Text.ToDouble();

                        tb = type.GetField("tbxFlow" + i.ToString(), Flags).GetValue(this) as TextBox;
                        iter.CuSlots[slot].Flow = tb.Text.ToInt();

                    }
                }


                if (!iter.RecipeViewVerify())
                {
                    return;
                }

                RecipeView.Update(iter);
                   
                Close();
            }
            else//新增
            {

                var iter = new RecipeView();
                iter.Name = txt料号.Text.Trim();

                try
                {
                    iter.Width = txt宽度.Text.ToDouble();

                    iter.D电流效率 = txt电镀效率.Text.ToDouble() / 100;
                    iter.Thickness = txt板厚.Text.ToDouble();
                    iter.D镀铜C面厚度 = txt镀铜C面厚度.Text.ToDouble();
                    iter.D镀铜S面厚度 = txt镀铜S面厚度.Text.ToDouble();

                    iter.D镀铜时间 = txt镀铜时间.Text.ToDouble();
                    iter.D镀铜速度mpmin = txt镀铜速度米每分.Text.ToDouble();

                    iter.C面电流密度ASD = txtC面电流密度.Text.ToDouble();
                    iter.S面电流密度ASD = txtS面电流密度.Text.ToDouble();


            //        iter.K卡板报警电压 = txt卡板报警电压.Text.ToDouble();
                    iter.C除油1频率 = txt除油1频率.Text.ToDouble();
                    iter.C除油2频率 = txt除油2频率.Text.ToDouble();

                    iter.Comment = rht备注.Text;

                    //镀铜
                    {
                        var type = this.GetType();
                        var Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                        for (int i = 1; i <= KorbeRTR.Config.镀铜段数; i++)
                        {
                            int slot = i - 1;

                            var tb = type.GetField("tbxCurrentCoefficient" + i.ToString(), Flags).GetValue(this) as TextBox;
                            iter.CuSlots[slot].D电流系数 = tb.Text.ToDouble();

                            tb = type.GetField("tbxFlow" + i.ToString(), Flags).GetValue(this) as TextBox;
                            iter.CuSlots[slot].Flow = tb.Text.ToInt();
                        }
                    }

                    if (!iter.RecipeViewVerify())
                    {
                        return;
                    }

                    iter.Time = DateTime.Now;
                    iter.User = Global.GetInstance().CurrentUser.UserName;

                    RecipeView.Add(iter);

                    {
                        var list = RecipeView.ListAll();
                        form.UpdateGrid(list);
                    }
                       
                    Close();
                        
                            
                }
                catch (Exception err)
                {
                    MessageBox.Show("错误提示:" + err.Message + err.StackTrace, "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btn取消_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txt宽度_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.Width = txt宽度.Text.ToDouble();

            txt宽度英寸.Text = CalcRecipe.WidthInch.ToString("f2");


            //txtC面分布率.Text = (CalcRecipe.C面分布率 * 100).ToString("f2");
            //txtS面分布率.Text = (CalcRecipe.S面分布率 * 100).ToString("f2");

            Update电流();
        }

        //private void txtC面分布率_TextChanged(object sender, EventArgs e)
        //{
        //    Update电流();
        //}

        //private void txtS面分布率_TextChanged(object sender, EventArgs e)
        //{
        //    Update电流();
        //}

        private void txt电镀效率_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.D电流效率 = txt电镀效率.Text.ToDouble() / 100;

            UpdateC面电流密度();
            UpdateS面电流密度();
        }

        public void Update电流()
        {
            var listC = new TextBox[] { tbxCCurrent1, tbxCCurrent2, tbxCCurrent3, tbxCCurrent4, tbxCCurrent5, tbxCCurrent6, tbxCCurrent7, tbxCCurrent8 };
            var listS = new TextBox[] { tbxSCurrent1, tbxSCurrent2, tbxSCurrent3, tbxSCurrent4, tbxSCurrent5, tbxSCurrent6, tbxSCurrent7, tbxSCurrent8 };

            for (int i = 0; i < Config.镀铜段数; i++)
            {
                listC[i].Text = CalcRecipe.CuSlots[i].C面电流.ToString("F1");
                listS[i].Text = CalcRecipe.CuSlots[i].S面电流.ToString("F1");
            }
        }

        private void txt镀铜C面厚度_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.D镀铜C面厚度 = txt镀铜C面厚度.Text.ToDouble();

            txt镀铜S面厚度.Text = CalcRecipe.D镀铜C面厚度.ToString();

            UpdateC面电流密度();
        }

        private void txt镀铜S面厚度_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.D镀铜S面厚度 = txt镀铜S面厚度.Text.ToDouble();

            UpdateS面电流密度();
        }

        private void txt镀铜时间_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.D镀铜时间 = txt镀铜时间.Text.ToDouble();

            txt镀铜速度米每分.Text = (Config.镀铜槽数 * 3 / CalcRecipe.D镀铜时间).ToString("F2");

            UpdateC面电流密度();
            UpdateS面电流密度();
        }

        private void txtC面电流密度_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.C面电流密度ASD = txtC面电流密度.Text.ToDouble();

            Update电流();
        }

        private void txtS面电流密度_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.S面电流密度ASD = txtS面电流密度.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient1_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[0].D电流系数 = tbxCurrentCoefficient1.Text.ToDouble();

            var type = this.GetType();
            var Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
            for (int i = 2; i <= KorbeRTR.Config.镀铜段数; i++)
            {
                int slot = i - 1;

                var tb = type.GetField("tbxCurrentCoefficient" + i.ToString(), Flags).GetValue(this) as TextBox;
                tb.Text = tbxCurrentCoefficient1.Text;
            }

            Update电流();
        }

        private void tbxCurrentCoefficient2_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[1].D电流系数 = tbxCurrentCoefficient2.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient3_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[2].D电流系数 = tbxCurrentCoefficient3.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient4_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[3].D电流系数 = tbxCurrentCoefficient4.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient5_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[4].D电流系数 = tbxCurrentCoefficient5.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient6_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[5].D电流系数 = tbxCurrentCoefficient6.Text.ToDouble();

            Update电流();
        }


        private void tbxCurrentCoefficient7_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[6].D电流系数 = tbxCurrentCoefficient7.Text.ToDouble();

            Update电流();
        }

        private void tbxCurrentCoefficient8_TextChanged(object sender, EventArgs e)
        {
            CalcRecipe.CuSlots[7].D电流系数 = tbxCurrentCoefficient8.Text.ToDouble();

            Update电流();
        }

        private void UpdateC面电流密度()
        {
            txtC面电流密度.Text = (CalcRecipe.D镀铜C面厚度 / (0.22 * CalcRecipe.D镀铜时间 * CalcRecipe.D电流效率)).ToString("F4");
        }

        private void UpdateS面电流密度()
        {
            txtS面电流密度.Text = (CalcRecipe.D镀铜S面厚度 / (0.22 * CalcRecipe.D镀铜时间 * CalcRecipe.D电流效率)).ToString("F4");
        }

        private void tbxFlow1_TextChanged(object sender, EventArgs e)
        {
            var type = this.GetType();
            var Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
            for (int i = 2; i <= KorbeRTR.Config.镀铜段数; i++)
            {
                int slot = i - 1;

                var tb = type.GetField("tbxFlow" + i.ToString(), Flags).GetValue(this) as TextBox;
                tb.Text = tbxFlow1.Text;
            }
        }

        private void txt除油1频率_TextChanged(object sender, EventArgs e)
        {
            txt除油2频率.Text = txt除油1频率.Text;
        }

    }

    public static partial class KorbeVCPExtersions
    {
        /// <summary>
        /// 配方参数校验
        /// </summary>
        public static bool RecipeViewVerify(this RecipeView view)
        {
            if (string.IsNullOrWhiteSpace(view.Name))
            {
                MessageBox.Show("料号不能为空", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.Width < 10 || view.Width > 750)
            {
                MessageBox.Show( "宽度范围10-750mm", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.D电流效率 <= 0 || view.D电流效率 > 100)
            {
                MessageBox.Show( "电流效率范围0-100", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.Thickness < 0.036 || view.Thickness > 1.6)
            {
                MessageBox.Show( "板厚范围0.036-1.6", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.D镀铜C面厚度 < 0)
            {
                MessageBox.Show( "镀铜C面厚度不能小于0", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.D镀铜S面厚度 < 0)
            {
                MessageBox.Show( "镀铜S面厚度不能小于0", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (view.D镀铜时间 <= 0)
            {
                MessageBox.Show("镀铜时间不能小于0", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //iter.D镀铜速度


            //if (view.C面电流密度ASD <= 0 || view.C面电流密度ASD > 2.6)
            //{
            //    MessageBox.Show("C面电流密度ASD范围0-2.6", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            //if (view.S面电流密度ASD <= 0 || view.S面电流密度ASD > 2.6)
            //{
            //    MessageBox.Show( "S面电流密度ASD范围0-2.6", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            //if (view.K卡板报警电压 < 0.1 || view.K卡板报警电压 > 5)
            //{
            //    MessageBox.Show( "卡板报警电压范围0.1-5", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            //if (view.C除油1频率 < 0.1 || view.C除油1频率 > 50)
            //{
            //    MessageBox.Show( "除油频率范围0.1-50", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            //if (view.C除油2频率 < 0.1 || view.C除油2频率 > 50)
            //{
            //    MessageBox.Show("除油频率范围0.1-50", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}


            //镀铜
            for (int i = 1; i <= KorbeRTR.Config.镀铜段数; i++)
            {
                int slot = i - 1;

                if (view.CuSlots[slot].D电流系数 < 0.1 || view.CuSlots[slot].D电流系数 > 1.5)
                {
                    MessageBox.Show( "电流系数范围0.1-1.5", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                if (view.CuSlots[slot].Flow < 0.1 || view.CuSlots[slot].Flow > 50)
                {
                    MessageBox.Show( "喷流频率范围0-50且为整数", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

            }

            return true;
        }
    }
}
